package ynu.edu.service;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ynu.edu.common.service.LogicService;
import ynu.edu.dao.*;
import ynu.edu.dto.OrderLineItemDTO;
import ynu.edu.dto.ShopLineItemDTO;
import ynu.edu.entity.*;
import ynu.edu.utils.UpdateTool;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CustomerService extends LogicService<ICustomerDao, Customer,Long> {
    public CustomerService(@Autowired ICustomerDao dao) {
        super(dao);
    }

    @Resource
    private IShopDao shopDao;
    @Resource
    private IOrderDao orderDao;
    @Resource
    private IShopItemDao shopItemDao;
    @Resource
    private ILineItemDao lineItemDao;

    public static Customer getCurrentCustomer(User user){
        Customer customer = new Customer();
        // 复制user中不为空的字段到customer中
        UpdateTool.copyNullProperties(user, customer);
        return customer;
    }

    public Order getUnpaidOrderByShop(User user,Long shopId){
        Customer customer = getCurrentCustomer(user);
        Shop shop = shopDao.getReferenceById(shopId);
        return customer.getUnpaidOrderByShop(shop);
    }


    public Order placeOrder(User user, OrderLineItemDTO orderLineItem){
        //下单功能，不存在则新建，存在则追加更新
        if (orderLineItem.getOrderId() != null){
            Long orderId = orderLineItem.getOrderId();
            Order order = orderDao.getReferenceById(orderId);
            //创建订单细则
            List<LineItem> lineItems = new ArrayList<>();
            for (ShopLineItemDTO shopLineItem : orderLineItem.getShopLineItems()) {
                if (shopLineItem.getQuantity()>0){
                    if (StringUtils.isNoneBlank(shopLineItem.getId().toString())){
                        LineItem lineItem = lineItemDao.getReferenceById(shopLineItem.getId());
                        lineItem.setQuantity(shopLineItem.getQuantity());
                        lineItems.add(lineItem);
                    }
                    else {
                        LineItem lineItem = new LineItem();
                        lineItem.setShopItem(shopItemDao.getReferenceById(shopLineItem.getShopItemId()));
                        lineItem.setQuantity(shopLineItem.getQuantity());
                        lineItems.add(lineItem);
                    }
                }
            }
            //更新订单
            return orderDao.save(order.updateOrder(lineItems));
        }
        Customer customer = getCurrentCustomer(user);
        //为空说明没有新订单
        Order order = new Order();
        //创建订单细则
        List<LineItem> lineItems = new ArrayList<>();
        //获取门店
        Shop shop = shopDao.getReferenceById(orderLineItem.getShopId());
        //获取lineitem
        for (ShopLineItemDTO shopLineItem : orderLineItem.getShopLineItems()) {
            if (shopLineItem.getQuantity() > 0){
                LineItem lineItem = new LineItem();
                ShopItem shopItem = shopItemDao.getReferenceById(shopLineItem.getShopItemId());
                lineItem.setShopItem(shopItem);
                lineItem.setQuantity(shopLineItem.getQuantity());
                lineItems.add(lineItem);
            }
        }
        //创建订单
        Order newOrder = order.createOrder(customer,shop, lineItems);
        Customer customer_new_order = customer.placeOrder(newOrder);
        //返回最新订单
         return orderDao.findFirstByCustomerOrderByIdDesc(getDAO().save(customer_new_order));

//        return orderDao.save(newOrder);
    }


    public Order payOrder(User user, Long orderId){
        Customer customer = getCurrentCustomer(user);
        Order order = orderDao.getReferenceById(orderId);
        Order order_pay = customer.payOrder(order);
        getDAO().save(customer);
        return  order_pay;
//        return orderDao.save(customer.payOrder(order));
    }

    public Order cancelOrder(User user, Long orderId){
        Customer customer = getCurrentCustomer(user);
        Order order = orderDao.getReferenceById(orderId);
        Order order_cancel = customer.cancelOrder(order);
        getDAO().save(customer);
        return order_cancel;
//        return orderDao.save(customer.cancelOrder(order));
    }
    public List<Order> getOrders(User user){
        Customer customer = getCurrentCustomer(user);
        return orderDao.findByCustomer(customer);
    }
}


